package reflect

import (
	"os"
	"path/filepath"
	"reflect"
	"runtime"
	"strings"
)

func ReflectHandle(instance string) interface{} {
	return ""
}

func ReflectPaymentHandle(instance string) interface{} {
	//if PaymentComponent[instance] != nil {
	//	typeInstance := reflect.ValueOf(PaymentComponent[instance]).Type()
	//	return reflect.New(typeInstance).Interface()
	//} else {
	//	panic(fmt.Sprintf("No Payment Named [%s].", instance))
	//}
	return ""
}

func IsInstance(item interface{}, typeList []reflect.Kind) bool {
	for _, tp := range typeList {
		if reflect.ValueOf(item).Kind() == tp {
			return true
		}
	}
	return false
}

func New(item interface{}) interface{} {
	return reflect.New(reflect.TypeOf(item)).Interface()
}

func GetPkgPath(instance interface{}) string {
	return reflect.TypeOf(instance).PkgPath()
}

func GetPkgName(instance interface{}) string {
	pkgName := reflect.TypeOf(instance).PkgPath()
	if strings.Contains(pkgName, "/") {
		return strings.Split(pkgName, "/")[strings.Count(pkgName, "/")]
	} else {
		return pkgName
	}
}

func GetFilePath() string {
	if _, file, _, ok := runtime.Caller(1); ok {
		return file
	} else {
		return ""
	}
}

func GetFileName(n ...int) string {
	dn := 2
	if len(n) == 1 {
		dn = n[0]
	} else if len(n) > 1 {
		panic("Arguments only one.")
	}
	if _, file, _, ok := runtime.Caller(dn); ok {
		return strings.Split(filepath.Clean(file), string(os.PathSeparator))[strings.Count(filepath.Clean(file), string(os.PathSeparator))]
	} else {
		return ""
	}
}
